<?php

if (class_exists('testDataPath'))
    return;

class MockDataObject {}

class testDataPath extends UnitTestCase {

    function setUp() {
        $this->data=array(
        'A' => 'Aaaaah !'
        ,'B' => array('be&ecirc;h','baie')
        ,'C'=>array(
            'un'=>1
            ,'deux'=>2
            ,'trois'=>3)
        ,'D'=>array()
        ,'null'=>null);
        $this->obj = new MockDataObject();
        $this->obj->var='value';
        $this->obj->data=$this->data;
        $this->obj->null=null;
    }

    function testGet() {
        $this->assertEqual(datapath::get($this->data,'A'), 'Aaaaah !');
        $this->assertEqual(datapath::get($this->data,'B.1'), 'baie');
        $this->assertEqual(datapath::get($this->data,'C.trois'), 3);
        // proprietes d'objet
        $this->assertEqual(datapath::get($this,'data.C.un'), 1, "could not get object member");
        // valeur inexistante
        $this->assertNull(datapath::get($this->data['D.0']), "can grab null value ?");

        $this->assertEqual(datapath::get($this->data['D']), array(), "can't grab empty array ?");
    }

    function testSet() {
        datapath::set($this->data,'Dede','D.d');
        $this->assertEqual($this->data['D']['d'], 'Dede', 'could not set a value');
        datapath::set($this,42,'solution');
        $this->assertEqual($this->solution, 42, 'could not set a member');
        datapath::set($this,42,'data.solution');
        $this->assertEqual($this->data['solution'], 42, 'could not set a value in a member');
        // print_r($this->data);
    }

    function testHas() {
        $this->assertTrue(
            Datapath::has($this->data,'C.deux')
            , "Datapath::has() should return true if key is set");
        $this->assertFalse(
            Datapath::has($this->data,'i.am.not.set')
            , "Datapath::has() should return false if key is not set");
        $this->assertTrue(
            Datapath::has($this->data,'null')
            , "Datapath::has() should return true if key is set but null");
        // now with objects
        $this->assertTrue(
            Datapath::has($this->obj,'var')
            , "Datapath::has() should return true if member is set");
        $this->assertFalse(
            Datapath::has($this->obj,'i.am.not.set')
            , "Datapath::has() should return false if member is not set");
        $this->assertTrue(
            Datapath::has($this->data,'null')
            , "Datapath::has() should return true if member is set but null");
    }
}
